{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-02-26T06:45:44.555672Z",
     "start_time": "2025-02-26T06:45:44.551838Z"
    }
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torch import nn\n",
    "\n",
    "\n",
    "class CenteredLayer(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "\n",
    "    def forward(self, X):\n",
    "        return X - X.mean()"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([-2., -1.,  0.,  1.,  2.])"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = CenteredLayer()\n",
    "layer(torch.FloatTensor([1, 2, 3, 4, 5]))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T06:45:45.053756Z",
     "start_time": "2025-02-26T06:45:45.048441Z"
    }
   },
   "id": "8abfbda9e433ec69",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T06:46:00.695991Z",
     "start_time": "2025-02-26T06:46:00.692294Z"
    }
   },
   "id": "6ac9631e2fdfbaae",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(3.7253e-09, grad_fn=<MeanBackward0>)"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = net(torch.rand(4, 8))\n",
    "Y.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T06:46:25.800866Z",
     "start_time": "2025-02-26T06:46:25.795019Z"
    }
   },
   "id": "eb2a06c71ca6e2d5",
   "execution_count": 8
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 5.4.2. 带参数的层"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "18c246f132df56cc"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "class MyLinear(nn.Module):\n",
    "    def __init__(self, in_units, units):\n",
    "        super().__init__()\n",
    "        self.weight = nn.Parameter(torch.randn(in_units, units))\n",
    "        self.bias = nn.Parameter(torch.randn(units,))\n",
    "    def forward(self, X):\n",
    "        # linear = torch.matmul(X, self.weight.data) + self.bias.data\n",
    "        linear = X @ self.weight.data + self.bias.data\n",
    "        return F.relu(linear)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T07:49:05.203501Z",
     "start_time": "2025-02-26T07:49:05.189542Z"
    }
   },
   "id": "c930fafe941936f0",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "Parameter containing:\ntensor([[-0.2107,  0.3360,  0.3039],\n        [ 0.0897, -0.9566, -0.2836],\n        [ 0.1139,  0.2796,  0.0738],\n        [-1.3372,  0.3481,  0.2129],\n        [-0.9574, -0.5885,  0.2564]], requires_grad=True)"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linear = MyLinear(5, 3)\n",
    "linear.weight"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2025-02-26T07:49:10.982389Z",
     "start_time": "2025-02-26T07:49:10.971458Z"
    }
   },
   "id": "ff6b5860870717b4",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "813f18118610deca"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
